package com.huayi.datagovernance.exception;

import cn.hutool.core.text.StrBuilder;
import com.huayi.datagovernance.vo.ResponseResult;
import common.parent.system.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.List;

/**
 * 全局异常处理器
 * @author wuch
 * @version 1.0
 * @date 2021/4/9 11:37
 */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    /**
     * 指定出现什么异常执行这个方法
     */
    @ExceptionHandler(BizException.class)
    public ResponseResult bizException(BizException e) {
        log.error(e.getMessage(), e);
        return ResponseResult.failed(e.getMessage());

    }


    /**
     * 指定出现什么异常执行这个方法
     */
    @ExceptionHandler(BusinessException.class)
    public ResponseResult BusinessException(BusinessException e) {
        log.error(e.getMessage(), e);
        return ResponseResult.failed(e.getMessage());
    }


    /**
     * 指定出现什么异常执行这个方法
     */
    @ExceptionHandler(Exception.class)
    public ResponseResult error(Exception e) {
        log.error(e.getMessage(), e);
        return ResponseResult.failed("服务器内部异常");
    }

    /**
     * 方法参数校验
     */
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public ResponseResult handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
        log.error(e.getMessage(), e);
        List<FieldError> fieldErrors = e.getBindingResult().getFieldErrors();
        if (!fieldErrors.isEmpty()){
            StrBuilder strBuilder = StrBuilder.create();
            fieldErrors.forEach(fieldError->{strBuilder.append(fieldError.getDefaultMessage()).append(";");});
            return ResponseResult.failed(strBuilder.toString());
            /*return new ResponseResult(false,
                    strBuilder.toString(),
                    null);*/
        }
        return ResponseResult.failed("接口参数校验处理器异常");
   /*     return new ResponseResult(false,
                "接口参数校验处理器异常",
                null);*/
        /*   return new ResponseResult(false,
                Objects.isNull(e.getBindingResult().getFieldError()) ? "接口参数校验处理器异常" : e.getBindingResult().getFieldError().getDefaultMessage(),
                null);*/
    }

    /**
     * ConstraintViolationException
     */
    /*@ExceptionHandler(ConstraintViolationException.class)
    public CommonResult handleConstraintViolationException(ConstraintViolationException e) {
        log.error(e.getMessage(), e);
        Set<ConstraintViolation<?>> violations = e.getConstraintViolations();
        for (ConstraintViolation<?> item : violations) {
            String message = ((PathImpl) item.getPropertyPath()).getLeafNode().getName() + item.getMessage();
            return CommonResult.failed(ResultCode.VALIDATE_FAILED, message);
        }
        return CommonResult.failed(ResultCode.VALIDATE_FAILED, ResultCode.VALIDATE_FAILED.getMessage());
    }*/
}
